<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="./local.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="keyword" @keyup.enter="handleEnter">
        <h3>历史搜索</h3><button>全部删除</button>
        <p v-for="item of historys">{{item}} 
            <button @click="handleDelete(item)">删除</button>
        </p>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                keyword: "",
                historys: []
            },
            methods: {
                handleEnter() {
                    console.log(this.keyword);
                    if (!this.historys.includes(this.keyword)) {
                        this.historys.push(this.keyword);
                        setLocalStorage("historys",this.historys);
                        this.keyword = "";
                    } else {
                        alert("值已经存在")
                    }
                },
                handleDelete(val){
                    var index = this.historys.indexOf(val);
                    this.historys.splice(index,1);
                    setLocalStorage("historys", this.historys);
                }
            },
        //    mounted() {
        //         var historys = getLocalStorage("historys");
        //         console.log(historys);
        //         if (historys) {
        //             this.historys = historys;
        //         }
        //     }
        })
    </script>
</body>

</html>